package com.danny.Controller;

import com.danny.mapper.CheckInMapper;
import com.danny.pojo.CheckIn;
import com.danny.pojo.Room;
import com.danny.pojo.RoomReserve;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Commit;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
@RestController
public class CheckInController {
    @Autowired
    private CheckInMapper checkInMapper;
    @Autowired
    private RoomController roomController;
    @Autowired
    private RoomReserveController roomReserveController;
    @GetMapping("queryAllCheckIn")
    public List<CheckIn> queryAllCheckIn(){
        return checkInMapper.queryAllCheckIn();
    }
    @GetMapping ("queryCheckInLimit")
    public List<CheckIn> queryCheckInLimit(String CheckInName,int page,int pageSize){
        return  checkInMapper.queryCheckInLimit(CheckInName,page,pageSize);
    }
    @GetMapping("queryCheckInLimitByNoParam")
    public List<CheckIn> queryCheckInLimitByNoParam(int page,int pageSize){
        return checkInMapper.queryCheckInLimitByNoParam(page,pageSize);
    }
    public CheckIn searchRoom(CheckIn checkIn,String state){
        if(checkIn.getRoomNo()!=0) {
            Room room = roomController.queryRoomByRoomNo(checkIn.getRoomNo());
           if(room!=null) {
               if(state.equals("直接入住")&&room.getState().equals("空房")) {
                   checkIn.setRtype(room.getRoomType());
                   checkIn.setNewPrice(room.getNewPrice());
                   checkIn.setPrice(room.getPrice());
                   room.setState("入住");
                   roomController.updateRoom(room);
               }
               else if(state.equals("预约入住")&&room.getState().equals("预定")){
                   checkIn.setRtype(room.getRoomType());
                   checkIn.setNewPrice(room.getNewPrice());
                   checkIn.setPrice(room.getPrice());
                   room.setState("入住");
                   roomController.updateRoom(room);
               }
               else return null;
           }
           else {
               return null;
           }
        }
        return checkIn;

    }

    public CheckIn searchReserve(int reserveNo){
        RoomReserve roomReserve=new RoomReserve();
        roomReserve.setReserveNo(reserveNo);
        List<RoomReserve> list=roomReserveController.queryRoomReserve(roomReserve);
        if(!list.isEmpty()) {
            roomReserve=list.get(0);
            if(roomReserve.getState()==1) {
                roomReserve.setState(2);System.out.println("22222222");
                roomReserveController.updateRoomReserve(roomReserve);

                return new CheckIn(roomReserve);
            }
            else {

                return null;
            }
        }
        else{
            return null;
        }

    }
//    public void cancelReserve(int reserveNo){
//        RoomReserve roomReserve=new RoomReserve();
//        roomReserve.setReserveNo(reserveNo);
//        List<RoomReserve> list=roomReserveController.queryRoomReserve(roomReserve);
//        if(!list.isEmpty()) {
//            roomReserve=list.get(0);
//                roomReserve.setState(1);
//                roomReserveController.updateRoomReserve(roomReserve);
//            }
//
//        }
//
//    }

    @GetMapping("addCheckIn")
    @Transactional
    @Commit
    public String addCheckIn(CheckIn checkIn,int reserveNo){ //开房
        CheckIn ch1,ch2;
        if(reserveNo!=0){//预定转入住

                ch1=searchReserve(reserveNo);
               if(ch1!=null) {
                   checkIn = ch1;
                   ch2=searchRoom(checkIn,"预约入住");
                   if ( ch2!= null)
                       checkIn = ch2;
                   else {
                       //cancelReserve(reserveNo);
                       throw new RuntimeException("房间不存在或已入住");
                      // return "房间不存在或已入住";
                   }
               }
               else{
                   throw new RuntimeException("预定信息不存在或已入住");
                  // return "预定信息不存在或已入住";
               }
        }
        else {      //直接入住
            ch1=searchRoom(checkIn,"直接入住");
            if ( ch1!= null)
                checkIn =ch1;
            else{
                throw new RuntimeException("房间不存在或已入住");
                //return "房间不存在或已入住";
            }

        }
      //  List<CheckIn> list=CheckInMapper.queryAllCheckIn();
        checkIn.setReachTime(new Timestamp(new java.util.Date().getTime()));
        if(checkInMapper.addCheckIn(checkIn)>0)return "inserted";
        return "error";
    }
    @GetMapping("updateCheckIn")    //更换房间
    @Transactional
    public String updateCheckIn(CheckIn checkIn) {
        CheckIn checkin = new CheckIn();
        checkin.setCheckinNo(checkIn.getCheckinNo());
        List<CheckIn> list = queryCheckInByMul(checkin);
        checkin = list.get(0);
        Room room = roomController.queryRoomByRoomNo(checkin.getRoomNo());
        CheckIn ch = searchRoom(checkIn, "直接入住");
        if (ch != null) {
            room.setState("空房");
            roomController.updateRoom(room);
            checkIn = ch;
        } else {
            throw new RuntimeException("房间不存在或已入住");
        //return "房间不存在或已入住";
       }
        if(checkInMapper.updateCheckIn(checkIn)>0){

            return "updated";
        }
        return "error";

    }
    @GetMapping("deleteCheckInById")
    public String deleteCheckInById(int id){
        if(checkInMapper.deleteCheckInById(id)>0)return "deleted";
        return "error";
    }

    @GetMapping("CheckInCount")
    public  int CheckInCount(){
        return checkInMapper.CheckInCount();
    }

    @GetMapping("CheckInCountHasInput")
    public  int CheckInCountHasInput(CheckIn checkIn){
        return checkInMapper.CheckInCountHasInput(checkIn);
    }

    @GetMapping("queryCheckInByMul")
    public List<CheckIn> queryCheckInByMul(CheckIn checkIn){
        return checkInMapper.queryCheckInByMul(checkIn);
    }

    @GetMapping("queryCheckInByMulLimit")
    public List<CheckIn> queryCheckInByMulLimit(CheckIn checkIn,int page,int pageSize){
        return checkInMapper.queryCheckInByMulLimit(checkIn,page,pageSize);
    }
}
